package com.wnhz.work.yz.adapter;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.activity.ProjectDetailsActivity;
import com.wnhz.work.yz.activity.RecordUsedListActivity;
import com.wnhz.work.yz.bean.BudgetPayListBean;
import com.wnhz.work.yz.bean.RecordUsedListBean;
import com.wnhz.work.yz.view.NoScrollListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/3/24.
 */
public class AllProjectr2Adapter extends BaseAdapter {
    private BaseActivity context;
    private List<RecordUsedListBean.InfoEntity> beanList = new ArrayList<>();
    private View.OnClickListener listener;
    private Project2ContentAdapter mAdapter;
    private List<RecordUsedListBean.InfoEntity.DataEntity> dataList = new ArrayList<>();

    public AllProjectr2Adapter(Context context, List<RecordUsedListBean.InfoEntity> beanList, View.OnClickListener listener) {
        this.context = (RecordUsedListActivity) context;
        this.listener = listener;
//        notifyDataSetChanged();
        if (beanList == null) {
            notifyDataSetChanged();
            return;
        }
        this.beanList = beanList;
    }

    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return beanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private int index = 0;

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        AllProjectViewHolder viewHolder;
        if (convertView == null) {
            FindView findView = new FindView().invoke();
            convertView = findView.getConvertView();
            viewHolder = findView.getViewHolder();

            convertView.setTag(viewHolder);
        } else {
            try {
                viewHolder = (AllProjectViewHolder) convertView.getTag();
            } catch (Exception e) {
                FindView findView = new FindView().invoke();
                convertView = findView.getConvertView();
                viewHolder = findView.getViewHolder();
            }
        }
        index = position;
        viewHolder.ll_click.requestFocus();

        viewHolder.tv_type.setTextColor(0xffe9573d);
        viewHolder.rl_type1.setVisibility(View.GONE);
        viewHolder.rl_type2.setVisibility(View.GONE);
        try {
            viewHolder.tv_type.setVisibility(View.GONE);
        } catch (Exception e) {
            return convertView;
        }
        String str = "项目名称:<font color=\"#e9573d\">" + beanList.get(position).getRecord_name() + "</font>";
        viewHolder.xiangmu_name.setText(Html.fromHtml(str));

        viewHolder.tv_project_code.setText("合同号:" + beanList.get(position).getBudget_no());

        viewHolder.tv_goods_number.setText("共" + beanList.get(position).getCount() + "套商品 合计：¥");
        viewHolder.tv_all_price.setText(beanList.get(position).getTotal_fee());

        dataList = beanList.get(position).getData();

        mAdapter = new Project2ContentAdapter(context, dataList, beanList.get(position).getBudget_id());
        viewHolder.my_listView.setAdapter(mAdapter);
//        viewHolder.my_listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//
//            }
//        });
        viewHolder.goto_pay.setVisibility(View.GONE);

        viewHolder.tv_add_goods.setOnClickListener(listener);
        viewHolder.tv_add_goods.setTag(position);
        viewHolder.tv_schedule.setOnClickListener(listener);
        viewHolder.tv_schedule.setTag(position);
        viewHolder.tv_end_project.setOnClickListener(listener);
        viewHolder.tv_end_project.setTag(position);
        viewHolder.tv_plan.setOnClickListener(listener);
        viewHolder.tv_plan.setTag(position);
        viewHolder.tv_cancel.setOnClickListener(listener);
        viewHolder.tv_cancel.setTag(position);
        viewHolder.ll_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, ProjectDetailsActivity.class);
                try {
                    intent.putExtra("tv_project_code", beanList.get(position).getRecord_name());

                    intent.putExtra("budget_id", beanList.get(position).getBudget_id());
                    context.startActivity(intent);
                } catch (Exception e) {
                    notifyDataSetChanged();
                }
            }
        });
        final AllProjectViewHolder finalViewHolder = viewHolder;

        return convertView;
    }

    private BudgetPayListBean.InfoEntity info;


    private static ImageView add_img;


    class AllProjectViewHolder {
        TextView tv_project_code;
        TextView tv_type;
        NoScrollListView my_listView;
        TextView tv_all_price;
        TextView tv_goods_number;
        TextView tv_add_goods;
        TextView tv_schedule;
        TextView tv_end_project;
        RelativeLayout rl_type1;
        RelativeLayout rl_type2;
        TextView tv_plan;
        TextView tv_cancel;
        LinearLayout ll_click;
        TextView xiangmu_name;
        TextView goto_pay;
    }

    private class FindView {
        private View convertView;
        private AllProjectViewHolder viewHolder;

        public View getConvertView() {
            return convertView;
        }

        public AllProjectViewHolder getViewHolder() {
            return viewHolder;
        }

        public FindView invoke() {
            viewHolder = new AllProjectViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.all_project_item, null);
            viewHolder.tv_project_code = (TextView) convertView.findViewById(R.id.tv_project_code);
            viewHolder.tv_type = (TextView) convertView.findViewById(R.id.tv_type);
            viewHolder.my_listView = (NoScrollListView) convertView.findViewById(R.id.my_listView);
            viewHolder.tv_all_price = (TextView) convertView.findViewById(R.id.tv_all_price);
            viewHolder.tv_goods_number = (TextView) convertView.findViewById(R.id.tv_goods_number);
            viewHolder.tv_add_goods = (TextView) convertView.findViewById(R.id.tv_add_goods);//新增商品
            viewHolder.tv_schedule = (TextView) convertView.findViewById(R.id.tv_schedule);//项目进度
            viewHolder.tv_end_project = (TextView) convertView.findViewById(R.id.tv_end_project);//终止项目
            viewHolder.rl_type1 = (RelativeLayout) convertView.findViewById(R.id.rl_type1);
            viewHolder.rl_type2 = (RelativeLayout) convertView.findViewById(R.id.rl_type2);
            viewHolder.tv_plan = (TextView) convertView.findViewById(R.id.tv_plan);//审核进度
            viewHolder.tv_cancel = (TextView) convertView.findViewById(R.id.tv_cancel);//取消订单
            viewHolder.ll_click = (LinearLayout) convertView.findViewById(R.id.ll_click);
            viewHolder.goto_pay = (TextView) convertView.findViewById(R.id.goto_pay);
            viewHolder.xiangmu_name = (TextView) convertView.findViewById(R.id.xiangmu_name);
            return this;
        }
    }


}
